package com.alibaba.citrus.cr.price.ability.functions.entity.activity;

import com.epoch.app.crpricecenter.dto.request.ActivityInfoRequest;
import com.epoch.app.crpricecenter.model.dto.QueryActivityRequest;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: qiaojiange
 * @create: 2021/11/20
 * @Description 组合货品存储
 */
@Data
public class CombinationItemStore {
    /**
     * 组合商品实体列表
     */
    private List<CombinationItemGroupByMainOrderIdStore> combinationItemEntityList;


    private CombinationItemStore() {

    }

    /**
     *
     * @param request 查询活动请求
     * @return
     */
    public static CombinationItemStore of(QueryActivityRequest request) {
        CombinationItemStore combinationItemStore = new CombinationItemStore();
        if (Objects.isNull(request) || CollectionUtils.isEmpty(request.getActivityInfoRequestList())) {
            return combinationItemStore;
        }

        Map<String,List<ActivityInfoRequest>> map = request.getActivityInfoRequestList().stream()
                .collect(Collectors.groupingBy(ActivityInfoRequest::getMainOrderId,Collectors.toList()));

        List<CombinationItemGroupByMainOrderIdStore> idStoreList = new ArrayList<>();
        map.entrySet().forEach(entry->{
            CombinationItemGroupByMainOrderIdStore store = new CombinationItemGroupByMainOrderIdStore(entry.getKey(),entry.getValue());
            idStoreList.add(store);
        });

        combinationItemStore.setCombinationItemEntityList(idStoreList);
        return combinationItemStore;

    }
}

